package m.jclecai.com.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.bean.GroupBuyBean;

/**
 * Created by hp on 2017/4/25.
 */

public class RecordAdapter extends BaseAdapter {
    private Context context;
    private List<GroupBuyBean> list;
    int index = 0;

    public RecordAdapter(Context context, List<GroupBuyBean> list, int index) {
        this.context = context;
        this.list = list;
        this.index = index;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_tou_recording, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        GroupBuyBean recordVoteBean = list.get(position);
        String addtime = recordVoteBean.getAddtime();
        String subtime = addtime.substring(5, 7);
        String subday = addtime.substring(8, 10);
        childViewHolder.tv_month.setText(subtime + "月");
        childViewHolder.tv_day.setText(subday);
        childViewHolder.tv_lottery.setText(recordVoteBean.getType());
        childViewHolder.tv_paying.setText(recordVoteBean.getMethodname());
        childViewHolder.tv_issue.setText("第" + recordVoteBean.getExpect() + "期");
        childViewHolder.tv_money.setText(recordVoteBean.getOne_money() + "元");
        if (index == 1)
            childViewHolder.tv_money.setText(recordVoteBean.getTrace_money() + "元");
        if ("2".equals(recordVoteBean.getTicket_id())) {
            childViewHolder.tv_ticket_mode.setText("已出票");//已出票
        } else {
            childViewHolder.tv_ticket_mode.setText("未出票");
        }
        childViewHolder.tv_draw_mode.setTextColor(ContextCompat.getColor(context,R.color.a));
        childViewHolder.tv_draw_mode.setText(recordVoteBean.getResult());
        String win = recordVoteBean.getWin_money();
        if(TextUtils.isEmpty(win))
            win = recordVoteBean.getTotal_winmoney();
        else if(Float.parseFloat(win) <= 0)
            win = recordVoteBean.getTotal_winmoney();
        if (!TextUtils.isEmpty(win) && Float.parseFloat(win) > 0) {
            childViewHolder.tv_draw_mode.setText("已中奖" + win + "元");
            childViewHolder.tv_draw_mode.setTextColor(ContextCompat.getColor(context,R.color.font_red));
        }
        return convertView;
    }

    static class ChildViewHolder {

        private final TextView tv_month;
        private final TextView tv_lottery;
        private final TextView tv_paying;
        private final TextView tv_issue;
        private final TextView tv_money;
        private final TextView tv_ticket_mode;
        private final TextView tv_draw_mode;
        private final TextView tv_day;

        public ChildViewHolder(View view) {
            tv_month = (TextView) view.findViewById(R.id.tv_month);
            tv_day = (TextView) view.findViewById(R.id.tv_day);
            tv_lottery = (TextView) view.findViewById(R.id.tv_lottery);
            tv_paying = (TextView) view.findViewById(R.id.tv_paying);
            tv_issue = (TextView) view.findViewById(R.id.tv_issue);
            tv_money = (TextView) view.findViewById(R.id.tv_money);
            tv_ticket_mode = (TextView) view.findViewById(R.id.tv_ticket_mode);
            tv_draw_mode = (TextView) view.findViewById(R.id.tv_draw_mode);
        }
    }
}
